package com.sktlab.bizconfmobile.activity;

import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.adapter.ParticipantListAdapter;
import com.sktlab.bizconfmobile.model.ConfControl;
import com.sktlab.bizconfmobile.model.ConfHistory;
import com.sktlab.bizconfmobile.model.ConferenceAccount;
import com.sktlab.bizconfmobile.model.Constant;
import com.sktlab.bizconfmobile.model.Participant;
import com.sktlab.bizconfmobile.model.manager.AccountsManager;
import com.sktlab.bizconfmobile.model.manager.CommunicationManager;
import com.sktlab.bizconfmobile.model.manager.ConfHistoryManager;
import com.sktlab.bizconfmobile.model.manager.ContactManager;
import com.sktlab.bizconfmobile.util.RunningInfoUtil;
import com.sktlab.bizconfmobile.util.Util;

public class ConferenceActivity extends BaseActivity implements OnClickListener{
	
	public static final String TAG = "ConferenceActivity";
	
	private Context mCtx;
	private ConferenceAccount mConfAccount;
	private ExpandableListView mElvParticipantlist;
	private ParticipantListAdapter mAdapter;
	
	private ConfHistory mHistory;
	
	private LinearLayout mLayoutForModerator1;
	private LinearLayout mLayoutForModerator2;
	private LinearLayout mLayoutForGuest;
	
	private final int LEAVE_CONF_DIALOG = 1001;
	private final int OTHER_FUNC_DIALOG = 1002;
	
	private int mDialogType = LEAVE_CONF_DIALOG;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_conference_main);
		
		initView(); 
	}
	
	private void initView() {
		
		mCtx = this;
			
		long id = getIntent().getLongExtra(Constant.KEY_OF_CONF_ACCOUNT_ID, -1L);
		
		mConfAccount = AccountsManager.getInstance().getAccountById(id);
		
		if (!Util.isEmpty(mConfAccount)) {
			
			setTitleName(mConfAccount.getConfAccountName());
		}		
		
		mLayoutForModerator1 = (LinearLayout) findViewById(R.id.layout_for_moderator_1);
		mLayoutForModerator2 = (LinearLayout) findViewById(R.id.layout_for_moderator_2);
		mLayoutForGuest = (LinearLayout) findViewById(R.id.layout_for_guest);
		
		do{
			
			if (Util.isEmpty(mConfAccount)) {
				
				break;
			}
			
			if (Util.isEmpty(mConfAccount.getModeratorPw())) {
				
				mLayoutForModerator1.setVisibility(View.GONE);
				mLayoutForModerator2.setVisibility(View.GONE);
				break;
			}
			
			mLayoutForGuest.setVisibility(View.GONE);
		
		}while(false);
		
		mElvParticipantlist = (ExpandableListView) findViewById(R.id.elv_participant_list);
		
		List<Participant> data = ContactManager.getInstance().getAllSelectedContacts();
		
		mAdapter = new ParticipantListAdapter(this,data);
		
		mElvParticipantlist.setAdapter(mAdapter);
	}
	
	@Override
	protected void onResume() {
		
		super.onResume();
		
		mAdapter.notifyDataSetChanged();
		
		mHistory = new ConfHistory();
		
		mHistory.setAccountID(String.valueOf(mConfAccount.getAccountId()));
		mHistory.setAccountName(mConfAccount.getConfAccountName());
		mHistory.setConfCode(mConfAccount.getConfCode());
		
		ConfHistoryManager chm = ConfHistoryManager.getInstance();
		
		do {
			
			chm.addHistory(mHistory);
			
		}while(false);	
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		mHistory.setEndDate(new Date());
		
		ConfHistoryManager.getInstance().updateDbRecord(mHistory);
		
		ConfControl.getInstance().disconnectToServer();
	}
		
	@Override
	public void finish() {
		
		String title = "alert";
		
		mDialogType = LEAVE_CONF_DIALOG;
		new AlertDialog.Builder(this).setMessage(title)
		.setTitle(title)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(R.string.toast_leave_conf, this)
		.setNegativeButton(android.R.string.cancel, this)
		.setNeutralButton(R.string.toast_finish_conf, this)
		.show();
		//super.finish();
	}

	public void onHfClicked(CheckedTextView hf) {
		
		if(hf.isChecked()) {
			
			Util.OpenSpeaker();
		}else {
			
			Util.CloseSpeaker();
		}	
	}
	
	public void onRollCallClicked(CheckedTextView rollCall) {
		
		if(rollCall.isChecked()) {
			
			ConfControl.getInstance().rollCall();
		}else {
			
		}	
	}
	
	public void onRecordClicked(CheckedTextView record) {
		
		if(record.isChecked()) {
			
			ConfControl.getInstance().record(1);
		}else {
			
			ConfControl.getInstance().record(0);
		}	
	}
	
	public void onLockClicked(CheckedTextView lock) {
		
		if(lock.isChecked()) {
			
			ConfControl.getInstance().lockConf(1);
		}else {
			
			ConfControl.getInstance().lockConf(0);
		}	
	}
	
	public void onMuteAllClicked(CheckedTextView muteAll) {
		
		if(muteAll.isChecked()) {
			
			ConfControl.getInstance().allMute(1,0);
		}else {
			
			ConfControl.getInstance().allMute(0,0);
		}	
	}
	public void onAddParticipant() {
		
		Util.startActivity(mCtx, AddParticipantActivity.class);
	}
	
	public void onSelfMuteClicked(CheckedTextView selfMute){
		
		if(selfMute.isChecked()) {
			
			ConfControl.getInstance().muteParty(ContactManager.getInstance().getCurrentUserObject(), 0, 0);
		}else {
			
			ConfControl.getInstance().muteParty(ContactManager.getInstance().getCurrentUserObject(), 1, 0);
		}	
	}
	
	public void onOtherFunClicked(CheckedTextView otherFunc){
		
//		Intent CallIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:")); 
//		/** Use NEW_TASK_LAUNCH to launch the Call Activity */ 
//		CallIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
//		/** Finally start the Activity */ 
//		startActivity(CallIntent); 
		
		//RunningInfoUtil.startSpecifiedApp(this,"com.android.phone");
		
		String title = "alert";
		
		mDialogType = OTHER_FUNC_DIALOG;
		
		new AlertDialog.Builder(this).setMessage("Please enter phone view to get more operation")
		.setTitle(title)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(R.string.toast_leave_conf, this)
		.setNegativeButton(android.R.string.cancel, this)
		.show();
	}
	
	public void onRadioClick(View v) {
		
		int id = v.getId();
		
		CheckedTextView ctv = null;
		
		if(v instanceof CheckedTextView) {
			
			ctv = (CheckedTextView) v;
			
			ctv.toggle();
		}else {
			
			return;
		}		
		
		switch (id) {

		case R.id.rb_open_hf:
		case R.id.ctv_guest_hf:
			//Util.shortToast(this, "hf");
			
			onHfClicked(ctv);
			
			break;
		case R.id.rb_self_mute:
		case R.id.ctv_guest_self_mute:
			//Util.shortToast(this, "self mute");
			onSelfMuteClicked(ctv);
			break;
		case R.id.rb_mute_all:
			onMuteAllClicked(ctv);
			break;
			
		case R.id.rb_call_name:
		case R.id.ctv_guest_roll_call:
			onRollCallClicked(ctv);
			//ctv.setChecked(false);
			break;
			
		case R.id.rb_record:
			onRecordClicked(ctv);
			break;
			
		case R.id.rb_add_participant:
			onAddParticipant();
			//ctv.setChecked(false);
			break;
			
		case R.id.rb_lock:
			onLockClicked(ctv);
			break;
			
		case R.id.rb_phone_transfer:
		case R.id.ctv_guest_transfer:
			break;
			
		case R.id.rb_other_function:
			
			onOtherFunClicked(ctv);
			break;
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		
		switch (which) {
		
		case DialogInterface.BUTTON_POSITIVE:
			
			if (mDialogType == LEAVE_CONF_DIALOG) {
				
				leaveConf();	
			}
					
			break;
			
		case DialogInterface.BUTTON_NEUTRAL:
			
			if (mDialogType == LEAVE_CONF_DIALOG) {
				
				finishConf();
			}
			
			break;
			
		case DialogInterface.BUTTON_NEGATIVE:
			
			break;		
		}
	}
	
	/**
	 * leave conference code here
	 */
	private void leaveConf() {
		
		super.finish();
	}
	
	/**
	 * finish conference code here
	 */
	private void finishConf() {
		
		super.finish();
	}
}
